package com.frank.auth.application.controller;

import com.frank.auth.application.convert.AuthRolePermissionDtoConvert;
import com.frank.auth.application.dto.AuthRolePermissionDto;
import com.frank.auth.domain.bo.AuthRolePermissionBo;
import com.frank.auth.domain.service.RolePermissionDomainService;
import com.frank.common.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/14
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/role/permission")
public class RolePermissionController {

    private final RolePermissionDomainService rolePermissionDomainService;

    public RolePermissionController(RolePermissionDomainService rolePermissionDomainService) {
        this.rolePermissionDomainService = rolePermissionDomainService;
    }


    @PostMapping(consumes = "application/json")
    public Result<Boolean> addRolePermission(@RequestBody AuthRolePermissionDto authRolePermissionDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController add role permission bind, params: {}", authRolePermissionDto);
            }
            Preconditions.checkNotNull(authRolePermissionDto.getRoleId(), "The role id cannot be empty!");
            Preconditions.checkArgument(!authRolePermissionDto.getPermissionIdList().isEmpty(), "The permission id list cannot be empty!");
            AuthRolePermissionBo authRolePermissionBo = AuthRolePermissionDtoConvert.INSTANCE.convertDtoToBo(authRolePermissionDto);
            return Result.ok(rolePermissionDomainService.addRolePermission(authRolePermissionBo));
        } catch (Exception e) {
            log.error("RolePermissionController add role permission bind error, params: {}, error: {}", authRolePermissionDto, e);
            return Result.fail("添加角色和权限绑定关系失败, Error: " + e.getMessage());
        }
    }


}
